package com.san.hkhospital.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.san.hkhospital.entity.Employee;
import com.san.hkhospital.service.EmployeeService;
import com.san.hkhospital.service.impl.EmployeeServiceImpl;
import com.san.hkhospital.vo.EmpTableVo;
import com.san.hkhospital.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
* (employee)表控制层
*
* @author xxxxx
*/
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    /**
     * 服务对象
     */
    @Resource
    private EmployeeServiceImpl employeeServiceImpl;
    @Autowired
    private EmployeeServiceImpl employeeService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Employee selectOne(Integer id) {
        return employeeServiceImpl.selectByPrimaryKey(id);
    }


    //************************************************************************************
//员工添加方法
    @RequestMapping("employeeAdd")
    public ResultVO employAdd(@RequestBody Employee employee
//                          @RequestParam("empName") String empName,
//                          @RequestParam("empGender") String empGender,
//                          @RequestParam("empIdcard") String empIdcard,
//                          @RequestParam("empTel") String empTel,
//                          @RequestParam("stateId") Integer stateId,
//                          @RequestParam("positionId") Integer positionId,
//                          @RequestParam("deptId") Integer deptId,
//                          @RequestParam("sectionId") Integer sectionId
    ) {
//    boolean flag = employeeService.employeeAdd(new Employee(null,empName, empGender, empIdcard, empTel, stateId, positionId, deptId, sectionId));
        boolean flag = employeeService.employeeAdd(employee);
        if (flag) {
            return ResultVO.success("员工添加完成！");
        }
        return ResultVO.failed("员工添加失败");
    }

    //员工删除方法
    @DeleteMapping("/{empId}")
    public ResultVO deleteemploy(@PathVariable Integer empId) {
        boolean flag = employeeService.deleteemploy(empId);
        if (flag) {
            return ResultVO.success("员工删除成功！");
        }
        return ResultVO.failed("员工删除失败！");
    }

    //员工状态修改
    @RequestMapping("changestateId")
    public ResultVO changestateId(@RequestParam Integer empId, @RequestParam Integer stateId) {
        boolean flag = employeeService.changeUstate(empId, stateId);
        if (flag) {
            return ResultVO.success("状态改变成功！");
        }
        return ResultVO.failed("状态改变失败！");
    }

    //员工全部修改
    @PostMapping("/updateemploy/{empId}")
    public ResultVO updateemploy(@PathVariable int empId, @RequestBody Employee employee) {
        employee.setEmpId(empId);
        int flag = employeeService.updateemploy(employee);
        if (flag == 0) {
            return ResultVO.failed("员工信息修改失败！");
        }
        return ResultVO.success("员工信息修改成功！");
    }

    //员工查询
//    @RequestMapping("selectEmploy")
//    public ResultVO selectEmploy(){
//        return ResultVO.success("员工查询成功", employeeService.selectEmployee());
//    }
    @RequestMapping("selectEmploy")
    public ResultVO selectEmploy(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Employee> employeeList = employeeService.selectEmployee();
        PageInfo<Employee> pageInfo = new PageInfo<>(employeeList);
        return ResultVO.success("员工查询成功", pageInfo);
    }

    @RequestMapping("selectEmploy1")
    public ResultVO selectEmploy() {
        List<Employee> employeeList = employeeService.selectEmployee();
        return ResultVO.success("员工查询成功",employeeList);
    }

    @RequestMapping("selectEmploy2")
    public ResultVO selectEmploy2() {
        List<Employee> allDoctList = employeeService.selectAllDoct();
        return ResultVO.success("所有医生查询成功",allDoctList);
    }

    @RequestMapping("selectEmployVo")
    public ResultVO selectEmployVo(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<EmpTableVo> employeeList = employeeService.selectEmployeeVo();
        PageInfo<EmpTableVo> pageInfo = new PageInfo<>(employeeList);
        return ResultVO.success("员工查询成功", pageInfo);
    }

    //搜索查询
    @GetMapping("/searchEmployee")
    public ResultVO searchEmployee(@RequestParam Map<String, String> params) {
        try {
            PageHelper.startPage(Integer.parseInt(params.get("pageNum")==null?"1":params.get("pageNum")), Integer.parseInt(params.get("pageSize")==null?"5":params.get("pageSize")));
            List<Employee> searchList = employeeService.searchList(params);
            PageInfo<Employee> pageInfo = new PageInfo<Employee>(searchList);
            return ResultVO.success("搜索查询成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failed("搜索查询失败");
        }
    }


}










































